package cl.xintec.utils.error
{
	import mx.utils.ObjectProxy;
	
	public class HandleError10
	{
		import mx.controls.Alert;
		import mx.rpc.events.FaultEvent;
		import mx.rpc.soap.SOAPFault;
		
		import mx.rpc.xml.SimpleXMLDecoder;
		import flash.xml.XMLDocument;
		import flash.utils.getTimer;
		
		import mx.utils.ObjectUtil;
		
		private var ERROR_DEFAULT_MSG:String = "Ha ocurrido un error";
		
		/**
		 * Excepción por omisión a atrapar
		 */
		private var _qNameException:String = "XintecException"; 
		
		/**
		 * Arreglo de mensajes arrojados por la capa de servicios
		 */
		private var _messages:Array = new Array();
		
		/**
		 * Ultimo codigo de error atrapado
		 */
		private var _lastCode:int = 0;
		
		/**
		 * Ultimo mensaje de error atrapado
		 */
		private var _lastMessage:String = "";
		
		/**
		 * Indica si se despliegan los mensajes atrapados
		 */
		private var _alertMessage:Boolean = true;
		
		public function HandleError10()
		{			
			_messages[138000] = "Información";
			_messages[200000] = "Información";			
			_messages[158000] = "Advertencia";
			_messages[178000] = "Error";
		}
		
		public function get qNameException():String {
			return _qNameException;
		}
		
		public function set qNameException(value:String):void {
			_qNameException = value;
		}
		
		public function get lastCode():int {
			return _lastCode;
		}
		
		public function get lastMessage():String {
			return _lastMessage;
		}
		
		public function get alertMessage():Boolean {
			return _alertMessage;
		}
		
		public function set alertMessage(value:Boolean):void {
			_alertMessage = value;
		}
		
		public function handleDeafaultFaults(event:FaultEvent):void {	    		
			/* Código Para imprimir mensaje de error */
			var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
			var xmlDoc:XMLDocument;
			var resultObj:Object;
			var objProxy:ObjectProxy;
			
			var title:String = ERROR_DEFAULT_MSG;
			var message:String;
			
			
			if (event.fault.faultDetail != null) {
				
				xmlDoc = new XMLDocument(event.fault.faultDetail);
				resultObj = decoder.decodeXML(xmlDoc);
				objProxy = resultObj as ObjectProxy;
				
				try {
					_lastCode = resultObj[_qNameException].code;
					title = _messages[resultObj[_qNameException].code];
					message = resultObj[_qNameException].message;
					_lastMessage = message;
					if (_lastMessage.indexOf("ORA-06512") > 0)
					{
						_lastMessage=_lastMessage.substring(40,_lastMessage.indexOf("ORA-06512"));
						message=_lastMessage;
					}
				} catch (err:Error) {
					try {
						message = event.fault.faultString;
						_lastMessage = message;
						
					} catch (err:Error) {
						message = resultObj as String;
						_lastMessage = message;
					}
				}
				
			} else {
				message = event.fault.faultString;
				_lastMessage = message;
			}
			
			
			if (title == null) {
				title = ERROR_DEFAULT_MSG;
			}
			
			if (_lastCode==20000){
				title="INFORMACION";
				message=message.substring(10,message.indexOf("ORA-06512"));
			}					
			
			if (_alertMessage) {
				if (message != "HTTP request error")	
					Alert.show(message, title);
			}
			
			trace(flash.utils.getTimer() + ": " + "HandleError10.handleDeafaultFaults: " + message);
		}
		
		
		/**
		 * @todo
		 * 
		 * Implementar para un manejo más detallado. 
		 */
		public function handleFaults(event:FaultEvent):void {
			Alert.show(ERROR_DEFAULT_MSG + event.fault.faultString, "Error");
			
			if (event.fault is SOAPFault) {
				var fault:SOAPFault=event.fault as SOAPFault;
				var faultElement:XML=fault.element;
				// You could use E4X to traverse the raw fault element returned in the SOAP envelope.
				// ...
				
			}			
		}
		
	}
}